package com.gzit.adapter;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * 由List<T>中的数据创建一个ListAdapter 使用CommonListAdapter
 * 
 * @author gzit
 * 
 * @param <T>
 */
@Deprecated
public abstract class CustomerListAdapter<T> extends BaseAdapter {

	protected List<T> mObjects = new LinkedList<T>();
	protected Context context;
	protected int itemLayoutId;

	public List<T> getmObjects() {
		return mObjects;
	}

	public void setmObjects(List<T> mObjects) {
		this.mObjects = mObjects;
	}

	public int getItemLayoutId() {
		return itemLayoutId;
	}

	public void setItemLayoutId(int itemLayoutId) {
		this.itemLayoutId = itemLayoutId;
	}

	public CustomerListAdapter(Context context, int itemLayoutId) {
		this.context = context;
		this.itemLayoutId = itemLayoutId;
	}

	public CustomerListAdapter(Context context) {
		this.context = context;
	}

	public void setItemLayout(int layoutID) {
		this.itemLayoutId = layoutID;
	}

	public void reset() {
		this.mObjects.clear();
		this.notifyDataSetChanged();
	}

	// 重新加载数据集
	public void reload(List<T> mObjects) {
		this.mObjects.clear();
		this.mObjects.addAll(mObjects);
		this.notifyDataSetChanged();
	}

	public void add(T item) {
		this.mObjects.add(item);
		this.notifyDataSetChanged();

	}

	public void addAll(List<T> items) {
		this.mObjects.addAll(items);
		this.notifyDataSetChanged();
	}

	public void remove(T item) {
		this.mObjects.remove(item);
		this.notifyDataSetChanged();
	}

	public void removeAll(List<T> all) {
		this.mObjects.removeAll(all);
		this.notifyDataSetChanged();
	}

	public int getCount() {
		return this.mObjects.size();
	}

	public T getItem(int position) {
		return this.mObjects.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	// 由子类负责，将某一特定的数据格式添加进view中
	protected abstract void render(T item, View view);

	public View getView(int position, View convertView, ViewGroup parent) {
		T item = this.getItem(position);

		// // 有可复用的view，则直接使用可复用的view
		if (convertView != null) {
			this.render(item, convertView);
			return convertView;
		}

		LayoutInflater inflater = LayoutInflater.from(context);
		View view = (View) inflater.inflate(itemLayoutId, null);
		this.render(item, view);
		return view;
	}
}
